﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 晨曦超市管理系统.收款1
{
    public partial class ReceivablesForm : Form
    {
        public ReceivablesForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }
        SqlDataReader root;
        private void ReceivablesForm_Load(object sender, EventArgs e)
        {
            //首先执行一次
            this.label2.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            textBox5.Text = Login.NAME;
            checkBox1.Checked = false;
            if (!checkBox1.Checked)
            {
                textBox16.Enabled = false;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void panel12_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //数量函数
        public void ss() {
            int ABC = 0;
            double ABc = 0;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                int a = int.Parse(listView1.Items[i].SubItems[6].Text);
                double b = double.Parse(listView1.Items[i].SubItems[7].Text);
                ABC += a;
                ABc += b;
                textBox1.Text = ABc.ToString();
                textBox2.Text = ABC.ToString();
            }
        }
        //判断我表里的商品
        SqlDataReader dr;
        private void button1_Click1(object sender, EventArgs e)
        {
            bool dui = true;//钩子变量
            string abc = "";
            double qian1 = 0;
            double ZeKou = 0;
            string AloneAmount = "";
            //判断我表里现在有没有一样的商品，有的话就在原来的商品数量加1金额也变动。把钩子变量改成fales就不会执行添加商品代码。
            for (int j = 0; j < listView1.Items.Count; j++)
            {
                abc = listView1.Items[j].SubItems[0].Text;//遍历
                if (textBox3.Text.Trim() == abc)
                {
                    string sqlstr = "select UnitPrice*Discount from CommodityList where ProductID='" + abc + "'";//查询我商品的价格。
                    dr = DBHelper.DateReader(sqlstr);
                    double zhao2 = 0;
                    while (dr.Read())
                    {
                        zhao2 = Convert.ToDouble(dr[0].ToString());
                    }
                    dr.Close();
                    int zhao = Convert.ToInt32(listView1.Items[j].SubItems[6].Text);//获取我表里的数量加1
                    zhao++;
                    listView1.Items[j].SubItems[6].Text = zhao.ToString();
                    double zhongqian = zhao2 * zhao;
                    listView1.Items[j].SubItems[7].Text = zhongqian.ToString();
                    dui = false;
                    break;
                }
            }
            if (dui)
            {
                string sqlstr = string.Format("select ProductID,Name1,UnitOfMeasurement,UnitPrice,Discount,UnitPrice*Discount,Shuliang from CommodityList where ProductID='{0}'", textBox3.Text.Trim());
                root = DBHelper.DateReader(sqlstr);

                while (root.Read())                                     //root是我定义的值，后面的read是找root里面的值找到一行执行一行，他是bool类型。
                {
                    double shoujia = Convert.ToDouble(root[5]);
                    ListViewItem item = new ListViewItem(root[0].ToString());  //root【】0就是代表第几列，也可以使用数据库里的列表名字做索引值。他返回的是一个object对象，我要转为tostring（）。
                    item.SubItems.Add(root[1].ToString());
                    item.SubItems.Add(root[2].ToString());
                    item.SubItems.Add(root[3].ToString());
                    item.SubItems.Add(root[4].ToString());
                    item.SubItems.Add(root[5].ToString());
                    item.SubItems.Add("1");
                    item.SubItems.Add(root[5].ToString());
                    item.SubItems.Add(root[6].ToString());
                    listView1.Items.Add(item);
                }
                root.Close();                                            //关闭我定义值的连接，然后在关闭数据库的连接
            }
            ss();
            qian1 = Convert.ToDouble(textBox1.Text.Trim());
            ZeKou = double.Parse(textBox8.Text.Trim());
            AloneAmount = (qian1 * ZeKou).ToString();
            label5.Text = AloneAmount;
        }

        private void ReceivablesForm_KeyDown(object sender, KeyEventArgs e)
        {
            //结算
            if (e.KeyCode == Keys.F12)
            {
                try
                {
                    string dan = DateTime.Now.ToString("yyyyMMddHHMMss");
                    string memberName = textBox4.Text.Trim(), UserNameID = Login.UserNameID;
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        string sqlstr = string.Format("insect into Sales(Salesman_Name,UserNameID,memberName)values('{0}','{1}','{2}')", dan, UserNameID, memberName);
                        DBHelper.QEX(sqlstr);
                        string ProductID = listView1.Items[i].SubItems[0].Text;
                        string Quantity = listView1.Items[i].SubItems[6].Text;
                        string AloneAmount = listView1.Items[i].SubItems[7].Text;
                        string sqlstr1 = string.Format("insect into SalesDetail(Salesman_Name,ProductID,Quantity,AloneAmount) values('{0}','{1}','{2}','{3}')", dan, ProductID, Quantity, AloneAmount);
                        DBHelper.QEX(sqlstr1);
                    }
                    listView1.Items.Clear();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            //数量的
            if (true)
            {
                //数量的
                int a = -1;
                if (listView1.SelectedIndices != null && listView1.SelectedIndices.Count > 0)
                {
                    a = int.Parse(listView1.SelectedItems[0].SubItems[6].Text);
                    --a;
                    double b = double.Parse(listView1.SelectedItems[0].SubItems[7].Text);
                    listView1.SelectedItems[0].SubItems[6].Text = a.ToString();
                    double c = a * b;
                    listView1.SelectedItems[0].SubItems[7].Text = c.ToString();
                }
                if (a == 0)
                {
                    listView1.SelectedItems[0].Remove();
                }
                if (listView1.SelectedIndices == null || listView1.SelectedIndices.Count < 0)
                {
                    MessageBox.Show("亲你没有选中行");
                }
            }
            //删除这一行
            if (true)
            {
                listView1.SelectedItems[0].Remove();
                if (listView1.SelectedIndices == null)
                {
                    MessageBox.Show("亲你没有选中行");
                }
            }
            //群删
            if (true)
            {
                listView1.Items.Clear();
            }
            //退出
            if (true)
            {
                Reusing.TuiChu();
            }
            //清除会员
            if (true)
            {

            }
            //切换用户
            if (true)
            {
                Login Register = new Login();
                Register.ShowDialog();
                this.Hide();
            }
        }

        private void button2_Click1(object sender, EventArgs e)
        {
            try
            {
                string dan = DateTime.Now.ToString("yyyyMMddHHMMss");
                string memberName = textBox4.Text.Trim(), UserNameID = Login.UserNameID;
                string sqlstr = string.Format("insert into Sales(Salesman_Name,UserNameID,memberName)values('{0}',{1},'{2}')", dan, UserNameID, memberName);
                bool dui2 = DBHelper.QEX(sqlstr);
                bool dui1 = true;
                string ProductID = "", Quantity = "", AloneAmount = "", AloneAmount1 = ""; ;
                double qian1 = 0, ZeKou = 0;
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                     ProductID = listView1.Items[i].SubItems[0].Text;
                     Quantity = listView1.Items[i].SubItems[6].Text;
                     qian1=double.Parse( textBox1.Text.Trim());
                     ZeKou =double.Parse(textBox8.Text.Trim());
                     AloneAmount = (qian1 * ZeKou).ToString();
                     AloneAmount1 = listView1.Items[i].SubItems[7].Text;
                    string sqlstr1 = string.Format("insert into SalesDetail(Salesman_Name,ProductID,Quantity,AloneAmount) values('{0}','{1}',{2},{3})", dan, ProductID, Quantity, AloneAmount1);
                    dui1= DBHelper.QEX(sqlstr1);
                    
                    if (checkBox1.Checked)
                    {
                       double A1=double.Parse( textBox16.Text.Trim());
                       string sqlstr3 = "update Menber set Balance-=" + A1 + ",xiaofei+=" + A1 + " where memberName='" + memberName + "'";
                       bool dui = DBHelper.QEX(sqlstr3);
                       string sqlstr4 = "selecrt xiaofei from Menber";
                       SqlDataReader dr = DBHelper.DateReader(sqlstr4);
                        int sql=0;
                       while (dr.Read())
                       {
                           sql = int.Parse(dr[0].ToString());
                       }
                       if (sql>1000)
                       {
                           string sqlstr5 = "update Menber set Grade='钻石',Discount=0.1 where memberName='" + memberName + "'";
                           bool duicuo = DBHelper.QEX(sqlstr5);
                       }
                    }
                }
                textBox12.Text = dan;//单号
                textBox13.Text = AloneAmount;//钱
                textBox14.Text = AloneAmount;//合计
                textBox15.Text = textBox2.Text;//数量
                label5.Text = AloneAmount;
                if (dui2 && dui1)
                {
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    MessageBox.Show("结算成功");
                }
                else
                {
                    MessageBox.Show("结算失败");
                }
                label5.Text = "";
                listView1.Items.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



          //  Login Register = new Login();
         //   Register.ShowDialog();
           // this.Hide();
        }

        private void button3_Click1(object sender, EventArgs e)
        {
            //数量的
            int a = -1;
            if (listView1.SelectedIndices != null && listView1.SelectedIndices.Count > 0)
            {
                a = int.Parse(listView1.SelectedItems[0].SubItems[6].Text);
                string abc = listView1.SelectedItems[0].SubItems[0].Text;
                --a;
                string sqlstr = "select UnitPrice*Discount from CommodityList where ProductID='" + abc + "'";//查询我商品的价格。
                dr = DBHelper.DateReader(sqlstr);
                double zhao2 = 0;
                while (dr.Read())
                {
                    zhao2 = Convert.ToDouble(dr[0].ToString());
                }
                dr.Close();
                listView1.SelectedItems[0].SubItems[6].Text = a.ToString();
                double c = a * zhao2;
                listView1.SelectedItems[0].SubItems[7].Text = c.ToString();
                ss();
            }
            if (a == 0)
            {
                listView1.SelectedItems[0].Remove();
            }
            if (listView1.SelectedIndices == null || listView1.SelectedIndices.Count < 0)
            {
                MessageBox.Show("亲你没有选中行");
            }
        }

        private void panel4_Paint(object sender, PaintEventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.label2.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        }
        SqlDataReader dr1;
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string a = textBox4.Text.Trim();
                string sqlstr = "select Mobile,Grade,Discount,Integral,Balance,xiaofei from Menber where memberName='" + a + "'";
                dr1 = DBHelper.DateReader(sqlstr);
                while (dr1.Read())
                {
                    textBox6.Text = dr1[0].ToString();
                    textBox7.Text = dr1[1].ToString();
                    textBox8.Text = dr1[2].ToString();
                    textBox9.Text = dr1[4].ToString();
                    textBox10.Text = dr1[3].ToString();
                    textBox11.Text = dr1[5].ToString();
                }

                dr1.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox16.Enabled = true;
            }
            else
            {
                textBox16.Enabled = false;
            }
        }
    }
}
